package bestTeam.WalkieDoggie.server;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jdo.PersistenceManagerFactory;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bestTeam.WalkieDoggie.client.walkieStatus;
import bestTeam.WalkieDoggie.client.Serializables.UserRequest;

public class NotifyWithImageServlet extends HttpServlet {

	private static final Logger LOG = Logger.getLogger(RequestServiceImpl.class
			.getName());

	@Override
	protected void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

		String owner = request.getParameter("owner_name");
		String owner_email = request.getParameter("owner_email");
		String walker = request.getParameter("walker");
		String imageEncoded64 = request.getParameter("image");
		String requestID = request.getParameter("requestID");
		
		byte[] imageData = Base64.decode(imageEncoded64);

		RequestServiceImpl requestService = new RequestServiceImpl();
		UserRequest req= requestService.getRequest(requestID);
		req.setStatus(walkieStatus.EXPIRED);
		requestService.UpdateRequest(req);

//		String body = createHtml(owner, owner_email, walker);
				
		String header = "Hi "+owner+"! Your dog got his walk!";
		String body = "Hi "+owner+"!\n\n"+walker+" has just notified that your dog got his walk!\n\n\n\nHave a nice day!";
		
		EmailServiceImpl emailService = new EmailServiceImpl();
		String res = emailService.sendEmailWithImage(owner_email, header,body,imageData);

		
		response.getWriter().print(res);
		
	}

	private String createHtml(String owner, String owner_email, String walker) {
		String myCSS = " style=\"color: #335983; font-family:comic sans MS;\"";
		String res="<div>";
		res+="<a href=\"https://walkie-doggie.appspot.com/\" target=\"_blank\"><img src=\"https://walkie-doggie.appspot.com/dailylogo.png\" alt=\"logo\"/></a>";
		res+="<h2"+myCSS+">Hi, "+owner+"!</h2>";
		res+="<h3"+myCSS+">"+walker+" has just notified that your dog got his walk!!\n";

		res+="<br/><br/><br/><a href=\"https://walkie-doggie.appspot.com/\" target=\"_blank\""+myCSS+">Visit WalkieDoggie website</a>";

		res+="</div>";

		return res;
	}



}
